﻿#region

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DeepEarth.Core.Utilities;


#endregion

namespace DeepEarth.Toolkit.Controls
{
    [TemplatePart(Name = PART_LoadingIndicator, Type = typeof(UIElement))]
    [TemplatePart(Name = PART_SearchBox, Type = typeof(TextBox))]
    public class FindControlToolPanel : Control
    {
        const string PART_LoadingIndicator = "PART_LoadingIndicator";
        const string PART_SearchBox = "PART_SearchBox";

        TextBox searchTermBox;

        public FindControlToolPanel()
        {
            DefaultStyleKey = typeof(FindControlToolPanel);

            TriggerSearchCommand = new LambdaCommand(() =>
                {
                    AreSearchResultsVisible = true;

                    if (SearchCommand != null && !string.IsNullOrEmpty(SearchTerm))
                    {
                        SearchCommand.Execute(SearchTerm);
                    }
                });

            AddHandler(FrameworkElement.KeyUpEvent, new KeyEventHandler(Panel_KeyUp), true);            
        }

        #region Properties
        public static readonly DependencyProperty SearchTermProperty = DependencyProperty.Register("SearchTerm", typeof(string), typeof(FindControlToolPanel), null);
        public string SearchTerm
        {
            get { return (string)GetValue(SearchTermProperty); }
            set { SetValue(SearchTermProperty, value); }
        }

        public static readonly DependencyProperty AreSearchResultsVisibleProperty = DependencyProperty.Register("AreSearchResultsVisible", typeof(bool), typeof(FindControlToolPanel), new PropertyMetadata(false, OnSearchResultsVisibleChanged));
        public bool AreSearchResultsVisible
        {
            get { return (bool)GetValue(AreSearchResultsVisibleProperty); }
            set { SetValue(AreSearchResultsVisibleProperty, value); }
        }

        static void OnSearchResultsVisibleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var panel = (FindControlToolPanel)sender;
            if (panel.ToggleSearchResultsCommand != null && panel.ToggleSearchResultsCommand.CanExecute(null))
                panel.ToggleSearchResultsCommand.Execute(args.NewValue);
        }
        #endregion

        #region Commands
        public static readonly DependencyProperty ToggleSearchResultsCommandProperty = DependencyProperty.Register("ToggleSearchResultsCommand", typeof(ICommand), typeof(FindControlToolPanel), null);
        public ICommand ToggleSearchResultsCommand
        {
            get { return (ICommand)GetValue(ToggleSearchResultsCommandProperty); }
            set { SetValue(ToggleSearchResultsCommandProperty, value); }
        }

        public static readonly DependencyProperty TriggerSearchCommandProperty = DependencyProperty.Register("TriggerSearchCommand", typeof(ICommand), typeof(FindControlToolPanel), null);
        public ICommand TriggerSearchCommand
        {
            get { return (ICommand)GetValue(TriggerSearchCommandProperty); }
            set { SetValue(TriggerSearchCommandProperty, value); }
        }

        public static readonly DependencyProperty SearchCommandProperty = DependencyProperty.Register("SearchCommand", typeof(ICommand), typeof(FindControlToolPanel), null);
        public ICommand SearchCommand
        {
            get { return (ICommand)GetValue(SearchCommandProperty); }
            set { SetValue(SearchCommandProperty, value); }
        }
        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            searchTermBox = (TextBox)GetTemplateChild(PART_SearchBox);
        }

        void Panel_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    if (TriggerSearchCommand != null)
                    {
                        // sync search terms
                        if (this.searchTermBox != null && this.SearchTerm != this.searchTermBox.Text)
                        {
                            this.SearchTerm = this.searchTermBox.Text;
                        }

                        TriggerSearchCommand.Execute(null);
                    }
                    break;
            }
        }
    }
}